package cn.bon.common.adapter;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.Module;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import jakarta.annotation.Resource;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @author: dronff
 * @description:
 * @CreateTime: 2023/12/9 11:42
 **/
@Component
@Data
@AllArgsConstructor
@NoArgsConstructor
public class JacksonJsonSerializerAdapter implements JsonSerializerAdapter{

    @Resource
    ObjectMapper objectMapper;

    @Override
    public byte[] readObjectAsJsonBytes(Object o) throws JsonProcessingException {
        return objectMapper.writeValueAsBytes(o);
    }

    @Override
    public String readObjectAsJson(Object o) throws JsonProcessingException {
        return objectMapper.writeValueAsString(o);
    }

    @Override
    public <T> T readJsonAsObject(String jsonStr, Class<T> clazz) throws JsonProcessingException {
        return objectMapper.readValue(jsonStr,clazz);
    }


}
